package com.hxl.demo.service.Impl;

import com.hxl.demo.entity.Post;
import com.hxl.demo.mapper.PostMapper;
import com.hxl.demo.service.PostService;
import com.hxl.demo.tools.Common;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Resource
    PostMapper postMapper;

    @Resource
    Post post;

    @Override
    public List<Post> Sel(Integer id, Integer limit, Integer page,String postName,String pinName,String deptName) {
        return postMapper.Sel(id,limit,page,postName,pinName,deptName);
    }

    @Override
    public Integer SelNum(String postName, String pinName, String deptName) {
        return postMapper.SelNum(postName,pinName,deptName);
    }

    public Integer postAdd(Post post){
        post.setInsertTime(new Date());
        return postMapper.postAdd(post);
    }
    public Object postUpdate(Integer id,String postName,String pinName){
        List<Post> postInfo=  postMapper.Sel(id,null,null,"","","");
        if(postInfo.size()<0){
            return Common.returnJsonFail("岗位信息不存在",400);
        }
        List<Post> postNoInfo=  postMapper.isNotSelf(id,postName);
        if(postNoInfo.size()>0){
            return Common.returnJsonFail("岗位名称已存在，不能重复",400);
        }
        post.setPostName(postName);
        post.setPinName(pinName);
        post.setId(id);

        Integer newId=postMapper.updatePost(post);
        if(newId>0){
            return Common.returnJsonFail("操作成功",200);
        }
        return Common.returnJsonFail("操作失败",400);

    }
    public Object postDelete(Integer id){
        Integer newId=postMapper.postDelete(id);
        if(newId>0){
            return Common.returnJsonFail("操作成功",200);
        }
        return Common.returnJsonFail("操作失败",400);
    }

}
